package cn.edu.cjw.APMFP.WorkStep.DAO;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.ResourceBundle;

import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;

import cn.edu.cjw.APMFP.Util.HBaseUtill;
import cn.edu.cjw.APMFP.Util.HiveUtill;
import cn.edu.cjw.APMFP.WorkStep.pojo.WorkStep;

public class WStepDAOImpl implements WStepDAO {

	public boolean addAndUpdateWStep(WorkStep ws) throws Exception {

		// 调用HBaseUtil工具获取连接
		Connection conn = HBaseUtill.getConn();

		// 获取表对象
		Table table = conn.getTable(TableName.valueOf(WorkStep.WORKSTEP_TABLE_NAME));

		// 写入ws对象
		Put put = HBaseUtill.putWStep(ws);
		
		table.put(put);

		// 释放资源
		HBaseUtill.Close(table, conn);

		return true;
	}

	public boolean removeWStep(WorkStep ws) throws Exception {
		// 调用HBaseUtil工具获取连接
		Connection conn = HBaseUtill.getConn();

		// 获取表对象
		Table table = conn.getTable(TableName.valueOf(WorkStep.WORKSTEP_TABLE_NAME));

		// 写入ws对象
		Delete delete = new Delete(Bytes.toBytes(ws.getWId()));
		table.delete(delete);

		// 释放资源
		HBaseUtill.Close(table, conn);

		return true;
	}

	public ArrayList<WorkStep> searchWStepByName(String name) throws Exception {

		ArrayList<WorkStep> list = new ArrayList<WorkStep>();

		ResourceBundle bundle = ResourceBundle.getBundle("System");
		String Hive_url = bundle.getString("Hive-url");
		String Hive_user = bundle.getString("Hive-user");
		String Hive_password = bundle.getString("Hive-password");

		// 获取Hive连接
		java.sql.Connection conn = HiveUtill.getConn(Hive_url, Hive_user, Hive_password);

		// 选取执行数据库
		String sql = "SELECT * FROM apmfp_workstep_table WHERE Wname=?";
		PreparedStatement ps = conn.prepareStatement(sql);

		ps.setString(1, name);
		// 执行SQL
		ResultSet rs = ps.executeQuery();

		// 处理数据
		while (rs.next()) {

			WorkStep workStep = new WorkStep(rs.getString("Wid"), rs.getString("Wname"), rs.getString("Wdescribe"));
			list.add(workStep);

		}

		// 释放资源
		HiveUtill.Close(rs, ps, conn);

		return list;
	}

	public WorkStep searchWStepById(String id) throws Exception {

		WorkStep workStep = new WorkStep();

		ResourceBundle bundle = ResourceBundle.getBundle("System");
		String Hive_url = bundle.getString("Hive-url");
		String Hive_user = bundle.getString("Hive-user");
		String Hive_password = bundle.getString("Hive-password");

		// 获取Hive连接
		java.sql.Connection conn = HiveUtill.getConn(Hive_url, Hive_user, Hive_password);

		// 选取执行数据库
		String sql = "SELECT * FROM apmfp_workstep_table WHERE Wid=?";
		PreparedStatement ps = conn.prepareStatement(sql);

		ps.setString(1, id);
		// 执行SQL
		ResultSet rs = ps.executeQuery();

		// 处理数据
		while (rs.next()) {

			workStep.setWId(rs.getString("Wid"));
			workStep.setWName(rs.getString("Wname"));
			workStep.setWDescribe(rs.getString("Wdescribe"));

		}

		// 释放资源
		HiveUtill.Close(rs, ps, conn);

		return workStep;
	}

	public ArrayList<WorkStep> showAll() throws Exception {

		ArrayList<WorkStep> list = new ArrayList<WorkStep>();

		ResourceBundle bundle = ResourceBundle.getBundle("System");
		String Hive_url = bundle.getString("Hive-url");
		String Hive_user = bundle.getString("Hive-user");
		String Hive_password = bundle.getString("Hive-password");

		// 获取Hive连接
		java.sql.Connection conn = HiveUtill.getConn(Hive_url, Hive_user, Hive_password);

		// 选取执行数据库
		String sql = "SELECT * FROM apmfp_workstep_table";
		Statement statement = conn.createStatement();
		// 执行SQL
		ResultSet rs = statement.executeQuery(sql);

		// 处理数据
		while (rs.next()) {

			WorkStep workStep = new WorkStep(rs.getString("Wid"), rs.getString("Wname"), rs.getString("Wdescribe"));
			list.add(workStep);

		}

		// 释放资源
		HiveUtill.Close(rs, statement, conn);

		return list;
	}

}
